package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.OrderCancelRules;
import com.chixing.entity.RoomImages;
import com.chixing.entity.Rooms;
import com.chixing.entity.Roomtype;
import com.chixing.mapper.*;
import com.chixing.service.IRoomsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-09-30
 */
@Service
public class RoomsServiceImpl  implements IRoomsService {

    @Autowired
    private RoomsMapper roomsMapper;
    @Autowired
    private RoomtypeMapper roomtypeMapper;
    @Autowired
    private RoomImagesMapper roomImagesMapper;
    @Autowired
    private OrderCancelRulesMapper orderCancelRulesMapper;

    @Override
    public Result getHotelRooms(Integer hotelId) {
        Map<Object , Object> map = new HashMap<>();
        //1.查询该酒店的房间类型
        QueryWrapper<Rooms> roomsQueryWrapper = new QueryWrapper<>();
        roomsQueryWrapper.select("distinct roomtype_id").eq("hotel_id",hotelId);
        List<Rooms> rooms = roomsMapper.selectList(roomsQueryWrapper);
        if (rooms.size()>0){
            List<Object> roomList = new ArrayList<>();
            for (Rooms room: rooms) {
                //存储一个房间信息
                List<Object> roomDetaiList = new ArrayList<>();
                //2.通过房间类型查询房间图片
                RoomImages roomImages = roomImagesMapper.selectById(room.getRoomtypeId());
                roomDetaiList.add(roomImages.getImageAddress());//房间图片
                //3.通过房间类型查询房间信息
                Roomtype roomtype = roomtypeMapper.selectById(room.getRoomtypeId());
                roomDetaiList.add(room.getRoomtypeId());//房间类型id
                roomDetaiList.add(roomtype.getRoomtypeName());//房间名称
                roomDetaiList.add(roomtype.getRoomtypeCapacity());//可以住几个人
                roomDetaiList.add(roomtype.getRoomtypeDes());//房间描述
                roomDetaiList.add(roomtype.getRoomArea());//房间面积
                roomDetaiList.add(roomtype.getRoomtypePrice());//房间价格
                //该房型的房间总数
                QueryWrapper<Rooms> queryWrapper = new QueryWrapper<>();
                queryWrapper.select("roomtype_id", "COUNT(room_id) AS roomCount")
                        .eq("hotel_id",hotelId)
                        .eq("roomtype_id",room.getRoomtypeId())
                        .groupBy("roomtype_id");
                List<Map<String, Object>> maps = roomsMapper.selectMaps(queryWrapper);
                for (Map<String, Object> m : maps) {
                    roomDetaiList.add(m.get("roomCount"));//房间总数
                }

                roomList.add(roomDetaiList);
            }
            map.put("roomList",roomList);
            //4.查询房间退改政策
            QueryWrapper<OrderCancelRules> orderCancelRulesQueryWrapper = new QueryWrapper<>();
            List<OrderCancelRules> orderCancelRules = orderCancelRulesMapper.selectList(orderCancelRulesQueryWrapper);
            List<String> orderCancelRuleList = new ArrayList<>();
            for (OrderCancelRules o: orderCancelRules) {
                orderCancelRuleList.add(o.getRuleDesc());
            }
            map.put("orderCancelRuleList",orderCancelRuleList);
        }
        return Result.getSuccess(map);
    }

}
